<template>
  <cloudPage :isLoading="!detail">
    <cloudHeader slot="gHeader" background="#1b1b2b">
      <view class="grace-header-body">
        <!-- 返回按钮 -->
        <view
          class="grace-header-icons grace-icons icon-arrow-left"
          style="color: #fff"
          @tap.stop="handleNavBack"
        ></view>
        <!-- 中间内容 -->
        <view
          class="grace-header-content-noflex grace-text-center"
          style="color: #fff; font-size: 36rpx"
        >
          {{ $t("mf.c2c.buy") }}
        </view>
        <!-- 右侧按钮 -->
        <view class="icons grace-icons"></view>
      </view>
    </cloudHeader>
    <view class="container" v-if="detail">
      <view class="top_box flex">
        <view class="title_bg" v-if="locale == 'zh_CN'">{{
          detail && detail.name
        }}</view>
        <view class="title_bg" v-else>{{ detail && detail.name_en }}</view>
        <view class="title_day"
          >{{ detail && detail.days }}{{ $t("mf.shop.day") }}</view
        >
      </view>
      <view class="center_box">
        <!-- <view class="box_title">{{ $t("mf.shop.amount") }}</view> -->
        <view class="flex center_list">
          <view class="amount_title">{{ $t("mf.shop.amount") }}</view>
          <input
            class="input_number"
            type="digit"
            v-model="number"
            :placeholder="
              $t('mf.shop.minimum', {
                number: detail.buy_min_num,
                symbol: detail.pay_symbol,
              })
            "
          />
        </view>
        <view class="able_num">{{
          $t("mf.shop.able", {
            symbol: detail.pay_symbol,
            number: coin_balance && coin_balance.balance,
          })
        }}</view>
      </view>
      <view class="explain">
        {{ $t("mf.shop.daoqihou") }}:{{ $t("mf.shop.explain1")
        }}{{ $t("mf.shop.explain2") }}{{ $t("mf.shop.explain3") }}
      </view>
      <view class="bottom_box">
        <view class="pay_type flex">
          <view class="flex1">{{ $t("mf.shop.pay_type") }}</view>
          <image
            :src="`/static/mf/home/coin/${detail.pay_symbol}.png`"
            class="pay_symbol"
          />
          <view>{{ detail.pay_symbol }}</view>
        </view>
      </view>
      <view class="agreement">
        <graceCheckBtn
          :checked="agreement"
          @change="checkedChange"
          checkedColor="#FFDB86"
          :size="32"
        >
          <text class="agreement_text">{{ $t("new.suanli_agree") }}</text>
        </graceCheckBtn>
      </view>
      <button
        class="pay_btn"
        @tap.stop="confirm"
        :loading="loading['shop.suanlibao_buy']"
      >
        {{ $t("mf.shop.confirm") }}
      </button>
    </view>
    <cloudPayPassword
      :show="showpay"
      @keyboardDone="payPasswordDone"
      @close="togglePayPassword"
    >
    </cloudPayPassword>
  </cloudPage>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      detail: null,
      number: null,
      agreement: false,
      coin_balance: null,
      showpay: false,
    };
  },
  async onLoad(option) {
    console.log(option.id);
    this.coin_balance = await this.coinBalance({ symbol: "FIL" });
    this.detail = await this.suanliInfo({
      id: option.id,
    });
  },
  methods: {
    ...mapActions("mf/shop", ["suanliInfo", "buySuanlibao"]),
    ...mapActions("mf/my", ["coinBalance"]),
    checkedChange() {
      this.agreement = !this.agreement;
    },
    togglePayPassword() {
      this.showpay = !this.showpay;
    },
    confirm() {
      console.log(this.number);
      if (!this.agreement) {
        this.makeToast({ title: "请同意协议" });
        return;
      }
      if (!this.number) {
        this.makeToast({ title: this.$t("mf.shop.number_toast") });
        return;
      }
      this.showpay = true;
    },
    async payPasswordDone(data) {
      this.togglePayPassword();
      await this.buySuanlibao({
        id: this.detail.id,
        number: this.number,
        pay_password: data.password,
      });
      this.number = null;
      this.makeToast({ title: "购买成功，请到算力宝订单中查看详情" });
    },
  },
  computed: {
    ...mapGetters("system", ["locale"]),
  },
};
</script>

<style scoped>
.container {
  width: 100%;
  padding: 20rpx;
  box-sizing: border-box;
}
.top_box {
  width: 100%;
  height: 120rpx;
  background: linear-gradient(270deg, #feecbf 0%, #ffdb86 100%);
  border-radius: 10rpx;
  padding: 0 44rpx;
  align-items: center;
  font-size: 32px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  margin-bottom: 30rpx;
  box-sizing: border-box;
}
.title_bg {
  width: 174rpx;
  height: 50rpx;
  text-align: center;
  background-image: url("/static/mf/shop/title_bg.png");
  background-size: 100% 100%;
  margin-right: 38rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #fee096;
}
.title_day {
  font-size: 30rpx;
  font-family: DINAlternate-Bold, DINAlternate;
  font-weight: bold;
  color: #22233d;
}
.coin_icon {
  width: 48rpx;
  height: 48rpx;
  margin-right: 20rpx;
}
.center_box {
  width: 100%;
  height: 224rpx;
  box-sizing: border-box;
  padding: 0 30rpx;
  background: linear-gradient(270deg, #1b1b2b 0%, #232534 100%);
  box-shadow: 0px 3px 4px 0px rgba(0, 0, 0, 0.5);
  border-radius: 12rpx;
}
.center_list {
  height: 120rpx;
  border-bottom: 1px solid #151935;
}
.amount_title {
  height: 120rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Light, PingFang SC;
  font-weight: 300;
  color: #ffffff;
  line-height: 120rpx;
  margin-right: 30rpx;
}
.input_number {
  flex: 1;
  height: 120rpx;
  line-height: 120rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
  box-sizing: border-box;
  margin-bottom: 30rpx;
}
.able_num {
  height: 100rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 80rpx;
}
.bottom_box {
  width: 100%;
  height: 112rpx;
  background: linear-gradient(270deg, #1b1b2b 0%, #232534 100%);
  box-shadow: 0px 3px 4px 0px rgba(0, 0, 0, 0.5);
  border-radius: 12px;
  box-sizing: border-box;
  padding: 30rpx;
  color: rgba(255, 255, 255, 0.5);
  line-height: 112rpx;
}
.explain {
  width: 100%;
  height: 100rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(255, 255, 255, 0.5);
  line-height: 100rpx;
  text-align: center;
}
.pay_type {
  height: 40rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  line-height: 40rpx;
}
.pay_symbol {
  width: 64rpx;
  height: 64rpx;
  margin-right: 20rpx;
  transform: translateY(-10rpx);
}
.agreement {
  box-sizing: border-box;
  padding: 40rpx 20rpx;
  margin-bottom: 150rpx;
}
.agreement_text {
  font-size: 24rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(255, 255, 255, 0.5);
  line-height: 40rpx;
}
.pay_btn {
  width: 516rpx;
  height: 100rpx;
  background-image: url("/static/auth/login/btn_bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  border-radius: 55rpx;
  line-height: 100rpx;
  font-size: 34rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #2b2c3e;
  position: fixed;
  bottom: 80rpx;
  left: 50%;
  transform: translateX(-50%);
}
</style>